﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors;
using MINERP.BLL.InfoConfig;
using MINERP.DAL;

namespace MINERP.InfoConfig.Controller
{
    public class UserRoleController
    {
        #region //private fields
        private UserRoleBLL      _bll = null;
        private GridControl             _grid = null;
        private GridView                _view = null;
        private ComboBoxEdit            _comboBox = null;
        private GroupControl            _groupControl = null;
        private CheckEdit               _checkEdit = null;
        private CheckedComboBoxEdit     _checkComboBoxEdit =null;
        private List<tb_Power>          _powerList = null;
        private tb_Power                _power = null;
        private List<tb_Tech>           _techList = null;
        private CheckEdit[]             checkEdit = null;
        private List<tb_Tech> selectedTechS = new List<tb_Tech>();

        #endregion


        #region //public Priorities
        internal CheckedComboBoxEdit CheckedComboBoxEdit
        {
            set
            {
                _checkComboBoxEdit = value;
            }
        }
        internal GroupControl GroupControl
        {
            set
            {
                _groupControl = value;
            }
        }
        internal CheckEdit CheckEdit
        {
            set
            {
                _checkEdit = value;
            }
        }
        internal ComboBoxEdit ComoBox
        {
            set
            {
                _comboBox = value;
            }
        }
        internal GridControl Grid
        {
            set
            {
                _grid = value;
            }
        }

        internal GridView View
        {
            set
            {
                this._view = value;
            }
            get
            {
                return _view;
            }
        }
        #endregion

        /// <summary>
        /// 获得单个角色的操作权限
        /// </summary>
        /// <param name="checkEdits"></param>
        /// <returns></returns>
        private List<tb_Tech> GetTechList(CheckEdit[] checkEdits)
        {
            List<tb_Tech> list = new List<tb_Tech>();
            foreach (CheckEdit checkEdit in checkEdits)
            {
                if (checkEdit.Checked)
                {
                    tb_Tech tech = _bll.GetTechObj(checkEdit.Text);
                    list.Add(tech);
                }
            }
            return list;
        }

        #region //Public Methods
        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="power"></param>
        public void AddRole(tb_Power power)
        {
            //List<tb_Tech> techs = GetTechList(checkEdit);
            //foreach (tb_Tech tech in techs)
            //{
            //    power.tb_Tech.Add(tech);
            //}

            foreach (var item in selectedTechS)
            {
                power.tb_Tech.Add(item);
            }
            _bll.AddRole(power);
            selectedTechS.Clear();

        }
        /// <summary>
        /// 更新角色
        /// </summary>
        /// <param name="power"></param>
        public void UpdateRole(tb_Power power)
        {
            //List<tb_Tech> techs = GetTechList(checkEdit);
            //_bll.UpdateRole(power, techs);
            _bll.UpdateRole(power, selectedTechS);
              //_groupControl.Controls.Clear();
              selectedTechS.Clear();
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="power"></param>
        public void DeleteRole(tb_Power power)
        {
            _bll.DeleteRole(power);
        }
        /// <summary>
        /// 获取所有角色信息
        /// </summary>
        public void GetDate()
        {
            _bll = new UserRoleBLL();
            _bll.GetPowerList();
            _grid.DataSource = _bll.PowerList;
        }


        /// <summary>
        /// 选择节点时时动态创建操作CheckEdit
        /// </summary>
        /// <param name="obj"></param>
        public void CreateConctrolsOfSelectTechs()
        {

            List<CheckedListBoxItem> list = new List<CheckedListBoxItem>();
            //_techList.Clear();
            int i = 0;
            //if (_checkComboBoxEdit != null)
            //{
                _groupControl.Controls.Clear();
                selectedTechS.Clear();
                foreach (CheckedListBoxItem item in _checkComboBoxEdit.Properties.Items)
                {
                    if (item.CheckState.ToString() == "Checked")
                    {
                        tb_Tech tech = _bll.GetTechObj(item.Value.ToString());
                        selectedTechS.Add(tech);
                        #region new

                        CheckEdit checkBox = new CheckEdit();

                        int x = 10;
                        int y = 40;

                        //checkEdit[i] = new CheckEdit();
                        checkBox.Name = "checkEdit" + i;
                        checkBox.Left = x + checkBox.Width * i;
                        checkBox.Top = y;
                        if (checkBox.Left >= _groupControl.Width)
                        {
                            checkBox.Top = y + checkBox.Height * i;
                        }
                        checkBox.Text = tech.TechName;
                        checkBox.Checked = true;


                        _groupControl.Controls.Add(checkBox);
                        i++;
                        #endregion


                        //_techList.Add(tech);
                    }
                //}
            }
            //checkEdit = new CheckEdit[_techList.Count];
            //_groupControl.Controls.Clear();
            //for (int i = 0; i < _techList.Count; i++)
            //{
            //    //int x = 10;
            //    //int y = 40;
            //    //checkEdit[i] = new CheckEdit();
            //    //checkEdit[i].Name = "checkEdit" + i;
            //    //checkEdit[i].Left = x + checkEdit[i].Width * i;
            //    //checkEdit[i].Top = y;
            //    //if (checkEdit[i].Left >= _groupControl.Width)
            //    //{
            //    //    checkEdit[i].Top = y + checkEdit[i].Height * i;
            //    //}
            //    //checkEdit[i].Text = _techList[i].TechName;
            //    //checkEdit[i].Checked = true;
            //    //_groupControl.Controls.Add(checkEdit[i]);

            //}
        }
        /// <summary>
        /// 加载窗体时动态创建操作CheckEdit
        /// </summary>
        /// <param name="power"></param>
        public void CreateConctrolsByPower(tb_Power power)
        {
            _techList = new List<tb_Tech>();
            _power = _bll.GetPowerAndTech(power.Name);

            foreach (tb_Tech tech in _power.tb_Tech)
            {
                _techList.Add(tech);
            }

            checkEdit = new CheckEdit[_techList.Count];
            _groupControl.Controls.Clear();
            for (int i = 0; i < _techList.Count; i++)
            {
                int x = 10;
                int y = 40;
                checkEdit[i] = new CheckEdit();
                checkEdit[i].Name = "checkEdit" + i;
                checkEdit[i].Left = x + checkEdit[i].Width * i;
                checkEdit[i].Top = y;
                if (checkEdit[i].Left >= _groupControl.Width)
                {
                    checkEdit[i].Top = y + checkEdit[i].Height * i;
                }
                checkEdit[i].Text = _techList[i].TechName;
                checkEdit[i].Checked = true;
                _groupControl.Controls.Add(checkEdit[i]);

            }
        }


        /// <summary>
        /// 绑定节点
        /// </summary>
        public void LinkchkcmbTech()
        {
            _techList = _bll.GetTechList();
            _checkComboBoxEdit.Properties.Items.Clear();
            _checkComboBoxEdit.Text = "请选择角色....";
            foreach (var tech in _techList)
            {
                _checkComboBoxEdit.Properties.Items.Add(tech.TechName);
            }
        }
        #endregion

    }
}
